<?php


namespace app\common\model;


class ProductNav extends Basemodel
{
    /**
     * @return 无限分类
     */
    public function product_catetree($data = null, $html = '|----')
    {
        $list = cache('product_catetree' . cookie('think_var'));
        if (empty($list)) {
            !empty($data) ? $this->where($data) : '';
            $tptc = $this->order('sort asc,id ASC')->select();
            $list = $this->sort($tptc, 0, 0, $html);
            cache('product_catetree' . cookie('think_var'), $list);
        }
        return $list;
    }

    public function product_sort($data, $pid = 0, $level = 0, $html = '|----')
    {
        static $arr = array();
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                $v['level'] = str_repeat($html, $level);
                $v['jibie'] = $level+1;
                $v['link'] = stripos($v['link'], "http") === 0 ? $v['link'] : $v['model'] . '-' . $v['id'];
                $arr[] = $v;
                $this->sort($data, $v['id'], $level + 1, $html);
            }
        }
        return $arr;
    }
}